package com.suanfa.collect;

/**
 * 给定正数数组，求和>=target的最短子集合的长度
 */
public class TotalK {

    public static void main(String[] args) {
        int[] arr = {2,3,1,2,4,3};
        int target = 7;
        System.out.println(getMinCollect(arr,target));
    }


    public static int getMinCollect(int[] arr, int target){
        int i=0, j=0;
        int sum = 0;
        int minLength = Integer.MAX_VALUE;

        while (j<arr.length){
            sum+=arr[j];
            while(sum >= target){
                sum = sum-arr[i];
                minLength = Math.min(minLength, j-i+1);
                i++;
            }
            j++;
        }
        return  minLength == Integer.MAX_VALUE? 0: minLength;
    }
}
